﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Common;
using System.Data.SqlClient;
using System.Linq;

using MvcCms.Data;


namespace MvcCmsProjectTracker.Data
{
    public class EFBugRepository : IBugRepository
    {
        #region Constructors
        private readonly MvcCmsEntities _entities;

        public EFBugRepository(IEntitiesProvider entitiesProvider)
        {
            _entities = entitiesProvider.GetEntityContext();
        }
        #endregion
        public void Save()
        {
            _entities.SaveChanges();
        }
        public Bug CreateBug(Bug bugToCreate)
        {
            _entities.Bugs.AddObject(bugToCreate);
            _entities.SaveChanges();
            return bugToCreate;
        }
        public Bug EditBug(Bug bugToEdit)
        {
            _entities.Bugs.ApplyCurrentValues(bugToEdit);
            _entities.SaveChanges();
            return bugToEdit;
        }

        public IQueryable<DtoTrackerIssue> ListIssues(int portalId)
        {
            return (from c in _entities.DtoTrackerIssues
                    where c.PortalID == portalId
                    orderby c.Title
                    select c);            
        }

        public IQueryable<Bug> GetBugsByProject(int projectID)
        {
            return from b in _entities.Bugs
                   .Include("AssignedTo")
                   .Include("ProjectStatus")
                   .Include("ProjectMilestone")
                   .Include("ProjectCategory")
                   .Include("ProjectPriority")
                   where b.ProjectID == projectID
                   && b.Deleted == false
                   orderby b.LastUpdate descending
                   select b;
        }

        public IQueryable<Bug> GetMyBugs(Guid userId)
        {
            return from b in _entities.Bugs
                   .Include("AssignedTo")
                   where b.AssignedTo.UserID== userId
                   && b.Deleted == false
                   orderby b.LastUpdate descending
                   select b;
        }

        public IQueryable<Bug> GetBugsByCategory(int projectID, int categoryID)
        {
            return from b in _entities.Bugs
                   .Include("AssignedTo")
                   where b.ProjectID == projectID
                   && b.CategoryID == categoryID
                   && b.Deleted == false
                   orderby b.LastUpdate descending
                   select b;
        }

        public IQueryable<Bug> GetBugsByMilestone(int projectID, int milestoneID)
        {
            return from b in _entities.Bugs
                   .Include("AssignedTo")
                   where b.ProjectID == projectID
                   && b.MilestoneID == milestoneID
                   && b.Deleted == false
                   orderby b.LastUpdate descending
                   select b;
        }

        public IQueryable<Bug> GetBugsByPriority(int projectID, int priorityID)
        {
            return from b in _entities.Bugs
                   .Include("AssignedTo")
                   where b.ProjectID == projectID
                   && b.PriorityID == priorityID
                   && b.Deleted == false
                   orderby b.LastUpdate descending
                   select b;
        }

        public IQueryable<Bug> GetBugsByStatus(int projectID, int statusID)
        {
            return from b in _entities.Bugs
                   .Include("AssignedTo")
                   where b.ProjectID == projectID
                   && b.StatusID == statusID
                   && b.Deleted == false
                   orderby b.LastUpdate descending
                   select b;
        }

        public Bug GetBug(int bugID)
        {
            return _entities.Bugs
                .Include("RelatedBugs")
                .Include("AssignedTo")
                .Include("Project")
                .Include("OriginatingTickets")
                .SingleOrDefault(b => b.BugID == bugID);
        }

        public void AddBug(Bug bug)
        {
            _entities.Bugs.AddObject(bug);
        }

        public void DeleteBug(Bug bug)
        {
            bug.Deleted = true;
        }

        public IQueryable<BugComment> GetBugComments(int bugID)
        {
            return from b in _entities.BugComments
                   where b.BugID == bugID
                   orderby b.DateCreated ascending
                   select b;
        }

        public BugComment GetBugComment(int commentID)
        {
            return _entities.BugComments.SingleOrDefault(b => b.CommentID == commentID);
        }

        public void AddBugComment(BugComment comment)
        {
            _entities.BugComments.AddObject(comment);
        }

        public void DeleteBugComment(BugComment comment)
        {
            _entities.BugComments.DeleteObject(comment);
        }

        public IQueryable<BugHistory> GetBugHistories(int bugID)
        {
            return from h in _entities.BugHistories
                   where h.BugID == bugID
                   orderby h.DateCreated descending
                   select h;
        }

        public BugHistory GetBugHistory(int historyID)
        {
            return _entities.BugHistories.SingleOrDefault(h => h.HistoryID == historyID);
        }

        public void AddBugHistory(BugHistory history)
        {
            _entities.BugHistories.AddObject(history);
        }

        public void DeleteBugHistory(BugHistory history)
        {
            _entities.BugHistories.DeleteObject(history);
        }

        public IQueryable<BugNotification> GetBugNotifications(int bugID)
        {
            return from b in _entities.BugNotifications
                   where b.BugID == bugID
                   select b;
        }

        public BugNotification GetBugNotification(int notificationID)
        {
            return _entities.BugNotifications.SingleOrDefault(n => n.NotificationID == notificationID);
        }

        public void AddBugNotification(BugNotification notification)
        {
            _entities.BugNotifications.AddObject(notification);
        }

        public void DeleteBugNotification(BugNotification notification)
        {
            _entities.BugNotifications.DeleteObject(notification);
        }

        public IQueryable<BugAttachment> GetBugAttachments(int bugID)
        {
            return from a in _entities.BugAttachments
                   where a.BugID == bugID
                   select a;
        }

        public BugAttachment GetBugAttachment(int attachmentID)
        {
            return _entities.BugAttachments.SingleOrDefault(a => a.AttachmentID == attachmentID);
        }

        public void AddBugAttachment(BugAttachment attachment)
        {
            _entities.BugAttachments.AddObject(attachment);
        }

        public void DeleteBugAttachment(BugAttachment attachment)
        {
            _entities.BugAttachments.DeleteObject(attachment);
        }
    }
}